package org.enwey.icarenew.Activity.Base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.NetworkError;
import com.android.volley.VolleyError;

import org.enwey.icarenew.API.RequestListResult;
import org.enwey.icarenew.API.RequestResult;
import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.Activity.Login.LoginActivity;
import org.enwey.icarenew.Activity.Personal.UserInfoActivity;
import org.enwey.icarenew.R;

import java.lang.ref.WeakReference;


/**
 * Created by enwey on 2014/11/15.
 */
public class BaseActivity extends FragmentActivity {
    private ImageView mUserInfo;
    private TextView mNoData;
    private RelativeLayout mLoading;
    private FrameLayout mContentLayout;
    private ImageView mLeftBtn;
    private ImageView mRightBtn;
    private LinearLayout mHeaderLayout;
    private TextView mTitle;
    private TextView mRightLabel;
    private ProgressDialog mProgressDialog;
    protected WeakReference<? extends Activity> weakThis = new WeakReference<Activity>(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(R.layout.base_activity);
        findView();
        LayoutInflater inflater = getLayoutInflater();
        inflater.inflate(layoutResID, mContentLayout, true);
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(R.layout.base_activity);
        findView();
        mContentLayout.addView(view);
    }

    private void findView() {
        mTitle = (TextView) this.findViewById(R.id.header_title);
        mContentLayout = (FrameLayout) this.findViewById(R.id.base_activity_frameLayout);
        mNoData = (TextView) this.findViewById(R.id.base_activity_no_data);
        mLoading = (RelativeLayout) this.findViewById(R.id.base_activity_load_layout);
        mUserInfo = (ImageView) this.findViewById(R.id.header_user_left);
        mLeftBtn = (ImageView) this.findViewById(R.id.header_btn_left);
        mRightBtn = (ImageView) this.findViewById(R.id.header_btn_right);
        mHeaderLayout = (LinearLayout) this.findViewById(R.id.app_header_layout);
        mRightLabel = (TextView) this.findViewById(R.id.header_right_label);

        setRegisterListener();
    }

    public void setHeaderColor(int colorId) {
        if (colorId != 0) {
            mHeaderLayout.setVisibility(View.VISIBLE);
            mHeaderLayout.setBackgroundResource(colorId);
        }
    }

    public void showHeader(boolean isShow) {
        if (isShow) {
            mHeaderLayout.setVisibility(View.VISIBLE);
        } else {
            mHeaderLayout.setVisibility(View.GONE);
        }
    }


    // 界面标题
    public void setTitle(String title) {
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText(title);
    }

    public void setTitle(int titleId) {
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText(titleId);
    }

    public void setTitle(int titleId, boolean isNew) {
        mTitle.setVisibility(View.VISIBLE);
        if (isNew) {
            String title = this.getString(titleId);
            mTitle.setText(title + "新闻");
        } else {
            mTitle.setText(titleId);
        }
    }

    public ProgressDialog showProgressDialog() {
        return showProgressDialog(null);
    }

    public ProgressDialog showProgressDialog(String massage) {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
        mProgressDialog = new ProgressDialog(this);
        if (!TextUtils.isEmpty(massage)) {
            mProgressDialog.setMessage(massage);
        }
        mProgressDialog.show();
        return mProgressDialog;
    }

    public void hideProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }

    public void showException(Exception e) {
        this.showException(e, null);
    }

    private void showException(Exception e, String message) {
        if (e instanceof NetworkError) {
            ToastUtil.show(this, "网络连接异常");
            return;
        }
        if (!TextUtils.isEmpty(message)) {
            ToastUtil.show(this, message);
            return;
        }
        if (e == null) {
            return;
        }
        if (e instanceof VolleyError) {
            if (TextUtils.isEmpty(e.getMessage())) {
                ToastUtil.show(this, "请求数据失败，请重试");
                return;
            }
        }
        ToastUtil.show(this, e.getMessage());
    }

    public boolean hasError(RequestResult<?> requestResult) {
        if (requestResult != null && requestResult.code == 200) {
            return false;
        }
        String message = requestResult != null ? requestResult.message : null;
        if (TextUtils.isEmpty(message)) {
            message = "请求数据失败，请重试";
        }
        ToastUtil.show(this, message);
        return true;
    }

    public boolean hasError(RequestListResult<?> requestListResult) {
        if (requestListResult != null && requestListResult.code == 200) {
            return false;
        }
        String message = requestListResult != null ? requestListResult.message : null;
        if (TextUtils.isEmpty(message)) {
            message = "请求数据失败，请重试";
        }
        ToastUtil.show(this, message);
        return true;
    }

    // 显示返回按钮
    public void showLeftBtn() {
        mLeftBtn.setVisibility(View.VISIBLE);
    }

    // 显示用户头像
    public void showLeftImg() {
        mUserInfo.setVisibility(View.VISIBLE);
    }

    // 显示右上角按钮
    public void showRightBtn(int resources) {
        if (resources == 0) {
            return;
        }
        mRightBtn.setImageResource(resources);
        mRightBtn.setVisibility(View.VISIBLE);
    }

    // 显示右上角文字
    public void showRightLabel(String label) {
        if (TextUtils.isEmpty(label)) {
            return;
        }
        mRightLabel.setText(label);
        mRightLabel.setVisibility(View.VISIBLE);
    }

    // 改变右上角文字颜色
    public void showRightLabelBeLight(boolean flag) {
        if (flag) {
            mRightLabel.setTextColor(Color.WHITE);
        } else {
            mRightLabel.setTextColor(Color.GRAY);
        }
    }

    // 显示加载数据
    public void showLoading() {
        mLoading.setVisibility(View.VISIBLE);
    }

    // 隐藏加载数据
    public void hideLoading() {
        mLoading.setVisibility(View.GONE);
    }

    // 显示没有数据
    public void showNoData() {
        mNoData.setVisibility(View.VISIBLE);
    }

    // 隐藏没有数据
    public void hideNoData() {
        mNoData.setVisibility(View.GONE);
    }

    // 按钮监听事件
    private void setRegisterListener() {
        View.OnClickListener register = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.header_btn_left:
                        finish();
                        break;
                }
            }
        };

        mLeftBtn.setOnClickListener(register);
    }

//    private class BroadcastReceiverHelper extends BroadcastReceiver {
//
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if (Constant.IGNORE_KICK_USER) {
//                return ;
//            }
//
//            String actionString = intent.getAction();
//            if (actionString != null) {
//                if (actionString.equals(Constant.RECEIVER_NO_LOGIN)) {
//                    if (!(BaseActivity.this instanceof SplashActivity)) {
//                        BaseActivity.this.finish();
//                        Intent showLoginIntent = new Intent(Constant.RECEIVER_SHOW_LOGIN);
//                        LocalBroadcastManager.getInstance(context).sendBroadcast(showLoginIntent);
//                    }
//                } else if (actionString.equals(Constant.RECEIVER_SHOW_LOGIN)) {
//                    showLoginView();
//                }
//            }
//        }
//    }

}
